import Cesium from '../../../Ces/Cesium';
import createFragmentFromTemplate from './createFragmentFromTemplate';

const loadView = (htmlString: string, container: string | HTMLElement, viewModel: any): Node[] => {
    container = Cesium.getElement(container) as HTMLElement;

    const fragment = createFragmentFromTemplate(htmlString);

    // Sadly, fragment.childNodes doesn't have a slice function.
    // This code could be replaced with Array.prototype.slice.call(fragment.childNodes)
    // but that seems slightly error prone.
    const nodes: Node[] = [];

    for (let i = 0; i < fragment.childNodes.length; ++i) {
        nodes.push(fragment.childNodes[i]);
    }

    container.appendChild(fragment);

    for (let i = 0; i < nodes.length; ++i) {
        const node = nodes[i];
        if (node.nodeType === 1 || node.nodeType === 8) {
            Cesium.knockout.applyBindings(viewModel, node);
        }
    }

    return nodes;
};

export default loadView;
